<template>
  <!-- 筛选区 -->
  <div class="sub-filter">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          href="javascript:;"
          v-for="item in filterData.brands"
          :key="item.id"
          :class="{ active: filterData.brands.selected === item.id }"
          @click="filterData.brands.selected = item.id"
        >
          {{ item.name }}
        </a>
      </div>
    </div>
    <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}：</div>
      <div class="body">
        <a
          href="javascript:;"
          v-for="sub in item.properties"
          :key="sub.id"
          :class="{ active: item.selected === sub.id }"
          @click="item.selected = sub.id"
        >
          {{ sub.name }}
        </a>
      </div>
    </div>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/api/category'
export default {
  name: 'SubFilter',
  setup() {
    const filterData = ref({})
    const route = useRoute()

    watch(
      () => route.params.id,
      async value => {
        if (!route.path.includes('/sub')) return
        // 发送请求获取数据
        const res = await findSubCategoryFilter(value)
        res.result.brands.unshift({ id: null, name: '全部' })
        // 默认选中的品牌的id
        res.result.brands.selected = null
        res.result.saleProperties.forEach(item => {
          item.properties.unshift({ id: null, name: '全部' })
          // 销售属性默认选中的id
          item.selected = null
        })
        filterData.value = res.result
      },
      {
        immediate: true
      }
    )
    return {
      filterData
    }
  }
}
</script>
<style scoped lang="less">
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
